<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java Switch语句的Case穿透现象</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: #282c34;
            border-radius: 12px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
            position: relative;
            overflow: hidden;
        }
        .code-header {
            background: #21252b;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 24px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #abb2bf;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #c678dd; }
        .string { color: #98c379; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; font-style: italic; }
        .function { color: #61afef; }
        .card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(0,0,0,0.15);
        }
        .flow-arrow {
            animation: flow 2s ease-in-out infinite;
        }
        @keyframes flow {
            0%, 100% { transform: translateX(0); }
            50% { transform: translateX(10px); }
        }
        .highlight-box {
            background: linear-gradient(135deg, #ffeaa7 0%, #fab1a0 100%);
            border-left: 4px solid #e17055;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl font-bold mb-4" style="font-family: 'Noto Serif SC', serif;">
                <i class="fas fa-code-branch mr-4"></i>Java Switch语句的Case穿透现象
            </h1>
            <p class="text-xl opacity-90 max-w-3xl mx-auto">
                深入理解switch语句中的控制流，避免常见的编程陷阱
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Code Example Section -->
        <section class="mb-16">
            <div class="grid md:grid-cols-2 gap-8 items-start">
                <div>
                    <h2 class="text-3xl font-bold mb-6 text-gray-800">
                        <i class="fas fa-laptop-code mr-3 text-purple-600"></i>代码示例
                    </h2>
                    <div class="code-container">
                        <div class="code-header">
                            <div class="code-dot bg-red-500"></div>
                            <div class="code-dot bg-yellow-500"></div>
                            <div class="code-dot bg-green-500"></div>
                            <span class="text-gray-400 ml-4 text-sm">Example.java</span>
                        </div>
                        <pre><code><span class="keyword">int</span> num = <span class="number">1</span>;

<span class="keyword">switch</span> (num) {
    <span class="keyword">case</span> <span class="number">0</span>:
        System.out.print(<span class="string">"0"</span>);
    <span class="keyword">case</span> <span class="number">1</span>:
        System.out.print(<span class="string">"1"</span>);
       <span class="comment">// break;</span>
    <span class="keyword">case</span> <span class="number">2</span>:
        System.out.print(<span class="string">"2"</span>);
    <span class="keyword">case</span> <span class="number">3</span>:
        System.out.print(<span class="string">"3"</span>);
    <span class="keyword">default</span>:
        System.out.print(<span class="string">"default"</span>);
}</code></pre>
                    </div>
                </div>

                <div>
                    <h2 class="text-3xl font-bold mb-6 text-gray-800">
                        <i class="fas fa-terminal mr-3 text-purple-600"></i>执行结果
                    </h2>
                    <div class="card p-8">
                        <div class="bg-gray-900 text-green-400 p-6 rounded-lg font-mono text-xl">
                            输出: <span class="text-yellow-300">123default</span>
                        </div>
                        <div class="mt-6 highlight-box p-4 rounded-lg">
                            <p class="text-gray-700">
                                <i class="fas fa-exclamation-triangle mr-2 text-orange-600"></i>
                                <strong>注意：</strong>这就是case穿透现象！当匹配到case 1后，由于没有break语句，程序会继续执行后续所有case中的代码。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Flow Diagram -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>执行流程图
            </h2>
            <div class="card p-8">
                <div class="mermaid">
                    graph TD
                        A[开始: num = 1] --> B{判断 num 值}
                        B -->|num == 0| C[case 0: 输出 '0']
                        B -->|num == 1| D[case 1: 输出 '1']
                        B -->|num == 2| E[case 2: 输出 '2']
                        B -->|num == 3| F[case 3: 输出 '3']
                        B -->|其他| G[default: 输出 'default']
                        
                        D --> E
                        E --> F
                        F --> G
                        G --> H[结束]
                        
                        style D fill:#ff6b6b,stroke:#c92a2a,stroke-width:3px
                        style E fill:#ffd43b,stroke:#fab005,stroke-width:2px
                        style F fill:#ffd43b,stroke:#fab005,stroke-width:2px
                        style G fill:#ffd43b,stroke:#fab005,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Key Concepts -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">
                <i class="fas fa-lightbulb mr-3 text-purple-600"></i>核心概念解析
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card p-6 hover:shadow-2xl">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-water"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">什么是Case穿透？</h3>
                    <p class="text-gray-600">
                        当switch语句匹配到某个case后，如果该case没有break语句，程序会继续执行下一个case的代码，直到遇到break或switch语句结束。
                    </p>
                </div>

                <div class="card p-6 hover:shadow-2xl">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">如何避免？</h3>
                    <p class="text-gray-600">
                        在每个case语句块的末尾添加break语句，确保执行完当前case后跳出switch语句，防止意外执行其他case的代码。
                    </p>
                </div>

                <div class="card p-6 hover:shadow-2xl">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-check-circle"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">最佳实践</h3>
                    <p class="text-gray-600">
                        始终在case语句块末尾添加break，除非你明确需要case穿透的行为。使用IDE的代码检查功能来发现遗漏的break语句。
                    </p>
                </div>
            </div>
        </section>

        <!-- Correct Code Example -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">
                <i class="fas fa-check mr-3 text-green-600"></i>正确的代码示例
            </h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card p-6">
                    <h3 class="text-xl font-bold mb-4 text-gray-700">
                        <i class="fas fa-code mr-2 text-green-600"></i>添加break语句
                    </h3>
                    <div class="code-container">
                        <pre><code><span class="keyword">switch</span> (num) {
    <span class="keyword">case</span> <span class="number">0</span>:
        System.out.print(<span class="string">"0"</span>);
        <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="number">1</span>:
        System.out.print(<span class="string">"1"</span>);
        <span class="keyword">break</span>;
    <span class="keyword">case</span> <span class="number">2</span>:
        System.out.print(<span class="string">"2"</span>);
        <span class="keyword">break</span>;
    <span class="keyword">default</span>:
        System.out.print(<span class="string">"default"</span>);
}</code></pre>
                    </div>
                </div>

                <div class="card p-6">
                    <h3 class="text-xl font-bold mb-4 text-gray-700">
                        <i class="fas fa-arrow-right mr-2 text-green-600"></i>执行结果
                    </h3>
                    <div class="bg-gray-900 text-green-400 p-6 rounded-lg font-mono text-xl mb-4">
                        输出: <span class="text-yellow-300">1</span>
                    </div>
                    <p class="text-gray-600">
                        <i class="fas fa-info-circle mr-2 text-blue-600"></i>
                        现在程序只会执行匹配的case，然后通过break语句跳出switch，避免了case穿透。
                    </p>
                </div>
            </div>
        </section>

        <!-- Tips Section -->
        <section class="mb-16">
            <div class="card p-8 bg-gradient-to-r from-purple-50 to-pink-50">
                <h2 class="text-2xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-star mr-3 text-yellow-500"></i>实用技巧
                </h2>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3 flow-arrow"></i>
                        <div>
                            <h4 class="font-bold mb-2">利用穿透特性</h4>
                            <p class="text-gray-600">有时可以故意利用case穿透来处理多个值执行相同操作的情况。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3 flow-arrow"></i>
                        <div>
                            <h4 class="font-bold mb-2">使用IDE提示</h4>
                            <p class="text-gray-600">现代IDE会警告缺少break语句的case，帮助避免意外的穿透。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                curve: 'basis'
            }
        });